package com.youlu.campus.website.controller;

import com.youlu.campus.common.base.QResult;
import com.youlu.campus.website.entity.ActivityDescription;
import com.youlu.campus.website.entity.ActivityDescriptionPageQuery;
import com.youlu.campus.website.service.ActivityDescriptionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 活动信息管理控制器
 */
@RestController
@RequestMapping("/website/activity")
public class ActivityDescriptionController {

    @Autowired
    private ActivityDescriptionService activityDescriptionService;

    /**
     * 新增或修改活动
     *
     * @param activity 活动对象
     * @return 响应结果，包含保存后的活动信息
     */
    @PostMapping("/save")
    public QResult<ActivityDescription> saveActivity(@RequestBody ActivityDescription activity) {
        if (StringUtils.isAnyBlank(activity.getTitle(), activity.getImageUrl())) {
            return QResult.fail("活动标题或者图片不能为null");
        }

        ActivityDescription savedActivity = activityDescriptionService.saveActivity(activity);
        return QResult.success(savedActivity);
    }

    /**
     * 根据 id 查询活动
     *
     * @param id 活动主键
     * @return 活动对象或 404 响应
     */
    @GetMapping("/info")
    public QResult<ActivityDescription> getActivityById(@RequestParam String id) {
        ActivityDescription activity = activityDescriptionService.getActivityById(id);
        return QResult.success(activity);
    }

    /**
     * 查询所有活动
     *
     * @return 活动列表
     */
    @PostMapping("/find-page")
    public QResult<Page<ActivityDescription>> findByPage(@RequestBody ActivityDescriptionPageQuery query) {
        Page<ActivityDescription> page = activityDescriptionService.findByPageToAdmin(query);
        return QResult.success(page);
    }

    /**
     * 根据 id 删除活动
     *
     * @param query 活动主键
     * @return 成功响应
     */
    @PostMapping("/delete")
    public QResult delete(@RequestBody ActivityDescriptionPageQuery query) {
        activityDescriptionService.deleteActivity(query.getId());
        return QResult.success();
    }
}